﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AnimationManagerWindow
{
    /// <summary>
    /// AnimationManagerView.xaml 的交互逻辑
    /// </summary>
    public partial class AnimationManagerView : UserControl
    {
        public ObservableCollection<TestModel> TestModels { get; set; }
        public AnimationManagerView()
        {
            
            TestModels = new ObservableCollection<TestModel>();
            TestModels.Add(new TestModel { Name = "item1" });
            TestModels.Add(new TestModel { Name = "item2" });
            TestModels.Add(new TestModel { Name = "item3" });
            TestModels.Add(new TestModel { Name = "item4" });
            TestModels.Add(new TestModel { Name = "item5" });
            InitializeComponent();

        }


        private parentItem FindVisualParent<parentItem>(DependencyObject obj) where parentItem : DependencyObject
        {
            if (obj == null)
            {
                return null;
            }
            obj = VisualTreeHelper.GetParent(obj);
            if (obj != null && obj is parentItem)
            {
                return (parentItem)obj;
            }
            else
            {
                obj = FindVisualParent<parentItem>(obj);
                if (obj != null)
                {
                    return (parentItem)obj;
                }
            }
            return null;
        }

        public childItem FindVisualChild<childItem>(DependencyObject obj) where childItem : DependencyObject
        {
            // Search immediate children first (breadth-first)
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(obj, i);

                if (child != null && child is childItem)
                    return (childItem)child;

                else
                {
                    childItem childOfChild = FindVisualChild<childItem>(child);

                    if (childOfChild != null)
                        return childOfChild;
                }
            }

            return null;
        }
        ListViewItem SelectItemContainer = null;
        Grid SelectedPanal = null;
        StackPanel ItemsPanel = null;
        bool ItemMoveStatus = false;
        Point ItemOriginPoint;
    




        private DependencyObject HitTest(DependencyObject obj, Point hPoint)
        {
            var visualObj = obj as Visual;
            if (visualObj == null)
            {
                return null;
            }
            var result = VisualTreeHelper.HitTest(visualObj, hPoint);
            return result.VisualHit;

        }
        private void StopDrag_Container(object sender, MouseButtonEventArgs e)
        {
            ItemMoveStatus = false;
        }

        private void DragItem_Drop(object sender, DragEventArgs e)
        {
            var obj = sender as ListView;
            var dropPoint = e.GetPosition(obj);
            var hitTarget = HitTest(obj, dropPoint);
            if (hitTarget == null)
            {
                return;
            }
            var viewItem = FindVisualParent<ListViewItem>(hitTarget);
            if (viewItem == null)
            {
                return;
            }
            var dropItemData = viewItem.DataContext as TestModel;
            var receivedData = e.Data.GetData(typeof(TestModel)) as TestModel;
            if (dropItemData == null || receivedData == null)
            {
                return;
            }
            var tindex = TestModels.IndexOf(dropItemData);
            TestModels.Remove(receivedData);

            TestModels.Insert(tindex, receivedData);

        }

        private void StartDragItem_ButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ButtonState == MouseButtonState.Pressed)
            {
                var testModel = ((Grid)sender).DataContext as TestModel;
                if (testModel is null)
                {
                    return;
                }
                DataObject data = new DataObject(testModel);
                DragDrop.DoDragDrop(this.DragableListView, data, DragDropEffects.Move);
            }
        }

        //private void Drag_container(object sender, MouseButtonEventArgs e)
        //{

        //    if(e.ButtonState==MouseButtonState.Pressed)
        //    {
        //        var aa=HitTest(this.DragableListView, e.GetPosition(this.DragableListView));
        //        //var dataContext = ((ListItem)sender).DataContext as TestModel;
        //        SelectItemContainer=FindVisualParent<ListViewItem>(sender as DependencyObject);
        //        SelectedPanal = FindVisualChild<Grid>(SelectItemContainer);
        //        //TestModels.Remove(dataContext);
        //        if (SelectItemContainer==null)
        //        {
        //            return;
        //        }
        //        ItemsPanel = FindVisualChild<StackPanel>(this.DragableListView);
        //        if (ItemsPanel==null)
        //        {
        //            return;
        //        }
        //        AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(ItemsPanel);
        //        if (adornerLayer==null)
        //        {
        //            return;
        //        }
        //        MoveAdorner = new MoveAdorner(ItemsPanel as UIElement);
        //        MoveAdorner.AddElement(SelectedPanal);
        //        adornerLayer.Add(MoveAdorner);
        //        var cursorPoint = e.GetPosition(ItemsPanel);
        //        ItemMoveStatus = true;
        //        Console.WriteLine($"x:{cursorPoint.X},y:{cursorPoint}");
        //        ItemOriginPoint = new Point(cursorPoint.X, cursorPoint.Y);
        //        //TestModels.Remove(dataContext);


        //    }
        //}
    }
    public class TestModel
    {
        public string Name { get; set; }
    }
}
